/**
 * 
 */
package com.yangtao.prototype.system.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.yangtao.framework.constant.Option;
import com.yangtao.framework.hibernate.BaseDao;
import com.yangtao.prototype.system.entity.Organize;

/**
 * @author 杨涛
 * 
 */
@Repository
public class OrganizeDAO extends BaseDao<Organize> {
	/**
	 * 取得所有子公司
	 * 
	 * @param id
	 * @return
	 */
	public List<Option> getCompanys(String id) {
		List<Option> ops = new ArrayList<Option>();
		Organize s = load(id);
		loadAllStructure(s, ops);
		return ops;
	}

	/**
	 * 递归取得所有子公司
	 * 
	 * @param s
	 * @param ops
	 */
	public void loadAllStructure(Organize s, List<Option> ops) {
		if (s != null) {
			ops.add(new Option(s.getId(), s.getName()));
			if (s.getSubOrganize() != null && s.getSubOrganize().size() > 0) {
				for (Organize st : s.getSubOrganize()) {
					loadAllStructure(st, ops);
				}
			}
		}
	}

	public List<Organize> findTop() {
		return find("from Structure s where s.supOrganize.id is null");
	}
}
